package com.nannybear.View;

import android.os.Handler;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.nannybear.R;

import java.util.Observable;
import java.util.Observer;

/**
 * @brief View of baby state
 */
public class BabyStateScreen extends Screen implements Observer {
    /********** DECLARATIONS **********/
    // Variables
    private int newStateValue;
    private int valueThresholdTemp;
    private int valueThresholdHum;
    private int NORMAL = 1;
    private int MODERATE = 2;
    private int CRITICAL = 3;

    // Widget of the view
    private RelativeLayout bbstate_screen_layout;
    private Timeline timeline;

    // Tab buttons
    private ImageButton homeButton;
    private ImageButton tempButton;
    private ImageButton brightnessButton;
    private ImageButton humidityButton;
    private ImageButton babyStateButton;
    private ImageButton settingsButton;
    private ImageButton helpButton;

    // Handler
    final Handler myHandler = new Handler();

    /************ METHODS ************/
    /**
     * @brief constructor.
     */
    public BabyStateScreen(){
        this.valueThresholdTemp = NORMAL;
        this.valueThresholdHum = NORMAL;
    }

    /**
     * @brief Reception of a new state value
     */
    public void update(Observable obs, Object obj){
        setNewStateValue(Integer.parseInt(obj.toString()));
        changeView();
    }

    /**
     * @brief Change the view with the new baby state
     */
    public void changeView() {
        myHandler.post(new Runnable() {
            @Override
            public void run() {
                int value = getNewStateValue();
                if(value == 1){
                    //TODO
                }
                if(value == 2){
                    //TODO
                }
            }
        });
    }

    /**
     * @brief Display the temperature tab with good color
     */
    public void displayTabTemp(){
        int thresholdTemp;
        thresholdTemp = getValueThresholdTemp();
        if (thresholdTemp == NORMAL) {
            getTempButton().setBackgroundResource(R.drawable.temp_grey);
        }
        if (thresholdTemp == MODERATE) {
            getTempButton().setBackgroundResource(R.drawable.temp_grey_moderate);
        }
        if (thresholdTemp == CRITICAL) {
            getTempButton().setBackgroundResource(R.drawable.temp_grey_critical);
        }
    }

    /**
     * @brief Display the humidity tab with good color
     */
    public void displayTabHum(){
        int thresholdHum;
        thresholdHum = getValueThresholdHum();
        if (thresholdHum == NORMAL) {
            getHumidityButton().setBackgroundResource(R.drawable.humidity_grey);
        }
        if (thresholdHum == MODERATE) {
            getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_moderate);
        }
        if (thresholdHum == CRITICAL) {
            getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_critical);
        }
    }

    /**
     * @brief Show the view
     */
    public void display(){
        displayTabTemp();
        displayTabHum();
        bbstate_screen_layout.setVisibility(View.VISIBLE);
        getBabyStateButton().setBackgroundResource(R.drawable.sound_active);
    }

    /**
     * @brief Close the view with the good color of temperature tab
     */
    public void closeTabTemp(){
        int thresholdTemp;
        thresholdTemp = getValueThresholdTemp();
        if (thresholdTemp == NORMAL) {
            getTempButton().setBackgroundResource(R.drawable.temp_grey);
        }
        if (thresholdTemp == MODERATE) {
            getTempButton().setBackgroundResource(R.drawable.temp_grey_moderate);
        }
        if (thresholdTemp == CRITICAL) {
            getTempButton().setBackgroundResource(R.drawable.temp_grey_critical);
        }
    }

    /**
     * @brief Close the view with the good color of humidity tab
     */
    public void closeTabHum(){
        int thresholdHum;
        thresholdHum = getValueThresholdHum();
        if (thresholdHum == NORMAL) {
            getHumidityButton().setBackgroundResource(R.drawable.humidity_grey);
        }
        if (thresholdHum == MODERATE) {
            getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_moderate);
        }
        if (thresholdHum == CRITICAL) {
            getHumidityButton().setBackgroundResource(R.drawable.humidity_grey_critical);
        }
    }

    /**
     * @brief Close the view
     */
    public void close(){
        closeTabTemp();
        closeTabHum();
        bbstate_screen_layout.setVisibility(View.GONE);
        getBabyStateButton().setBackgroundResource(R.drawable.sound_grey);
    }


    /*********** ACCESSORS ***********/
    public void setHomeButton(ImageButton homeBtn) {
        this.homeButton = homeBtn;
    }

    public ImageButton getHomeButton() {
        return homeButton;
    }

    public void setTempButton(ImageButton tempBtn) {
        this.tempButton = tempBtn;
    }

    public ImageButton getTempButton() {
        return tempButton;
    }

    public void setBrightnessButton(ImageButton brightnessBtn) {
        this.brightnessButton = brightnessBtn;
    }

    public ImageButton getBrightnessButton() {
        return brightnessButton;
    }

    public void setHumidityButton(ImageButton humidityBtn) {
        this.humidityButton = humidityBtn;
    }

    public ImageButton getHumidityButton() {
        return humidityButton;
    }

    public void setBabyStateButton(ImageButton babyStateBtn) {
        this.babyStateButton = babyStateBtn;
    }

    public ImageButton getBabyStateButton() {
        return babyStateButton;
    }

    public void setSettingsButton(ImageButton settingsBtn) {
        this.settingsButton = settingsBtn;
    }

    public ImageButton getSettingsButton() {
        return settingsButton;
    }

    public void setHelpButton(ImageButton helpBtn) {
        this.helpButton = helpBtn;
    }

    public ImageButton getHelpButton() {
        return helpButton;
    }

	public RelativeLayout getBbstate_screen_layout() {
		return bbstate_screen_layout;
	}

	public void setBbstate_screen_layout(RelativeLayout bbstate_screen_layout) {
		this.bbstate_screen_layout = bbstate_screen_layout;
	}

    public Timeline getTimeline() {
        return timeline;
    }

    public void setTimeline(Timeline timeline) {
        this.timeline = timeline;
    }

    public int getNewStateValue() {
        return newStateValue;
    }

    public void setNewStateValue(int newStateValue) {
        this.newStateValue = newStateValue;
    }

    public int getValueThresholdTemp() {
        return valueThresholdTemp;
    }

    public void setValueThresholdTemp(int valueThresholdTemp) {
        this.valueThresholdTemp = valueThresholdTemp;
    }

    public int getValueThresholdHum() {
        return valueThresholdHum;
    }

    public void setValueThresholdHum(int valueThresholdHum) {
        this.valueThresholdHum = valueThresholdHum;
    }
}
